package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@Api(tags = "员工管理相关接口")
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation(value = "员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation(value = "员工登出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param employeeDTO 员工信息
     */
    @ApiOperation(value = "新增员工")
    @PostMapping
    public Result addEmployee(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.addEmployee(employeeDTO);
        return Result.success();
    }

    /**
     * 分页查询员工
     * @param employeePageQueryDTO 员工分页查询条件
     * @return 员工分页查询结果
     */
    @ApiOperation(value = "员工分页查询")
    @GetMapping("/page")
    public Result<PageResult> getEmployeePage(EmployeePageQueryDTO employeePageQueryDTO) {
        // 使用 pageHelper 插件开始分页
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        // 查询分页结果
        Page<Employee> page = employeeService.getEmployeePage(employeePageQueryDTO);
        // 返回分页结果
        return Result.success(new PageResult(page.getTotal(), page.getResult()));
    }

    /**
     * 根据 ID 查询员工
     * @param id 员工主键
     * @return 员工详情
     */
    @ApiOperation(value = "根据 ID 查询员工")
    @ApiImplicitParam(name = "id", value = "员工主键", required = true, example = "1", paramType = "path", dataType = "long")
    @GetMapping("/{id}")
    public Result<Employee> getEmployeeById(@PathVariable Long id) {
        Employee employee = employeeService.getEmployeeById(id);
        return Result.success(employee);
    }

    /**
     * 更新员工状态
     * @param status 员工状态（1开启 0关闭）
     * @param id 员工主键
     */
    @ApiOperation(value = "更新员工状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "员工状态（1开启 0关闭）", required = true, example = "1", paramType = "path", dataType = "string"),
            @ApiImplicitParam(name = "id", value = "员工主键", required = true, example = "1", paramType = "query", dataType = "int")
    })
    @PostMapping("/status/{status}")
    public Result updateEmployeeStatus(@PathVariable Integer status, @RequestParam Long id) {
        employeeService.updateEmployeeStatus(status, id);
        return Result.success();
    }

    /**
     * 更新员工信息
     * @param employeeDTO 员工信息
     */
    @ApiOperation(value = "更新员工信息")
    @PutMapping
    public Result updateEmployee(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.updateEmployee(employeeDTO);
        return Result.success();
    }

}
